<?php
namespace Modules\ShopApi\Models;
class ShopProductType extends BaseApiModel
{	
	/**
     * @name  关联图片
     */
    public function image_one()
    {
        return $this->hasOne('Modules\Admin\Models\Image','id','image_id');
    }
	/**
     * @name  首页商品分类
     */
	public function product_type(){
		$where = ['level'=>3,'status'=>1,'open'=>1];
		$data = $this->select('id','title','image_id')
				->with([
				'image_one'=>function($query){
					$query->select('id','url','open');
				}      
				]) 
				->orderBy('sort','asc')
				->orderBy('id','desc')
				->where($where)
				->get()
				->toArray();
		$data = $this->image_list($data);		
		return $this->api_success(['data'=>$data]);		
	}
	/**
     * @name  商品一级分类
     */
	public function product_type_one(){
		$where = ['level'=>1,'status'=>1,'pid'=>0];
		$data = $this->select('id','title')
				->orderBy('sort','asc')
				->orderBy('id','desc')
				->where($where)
				->get()
				->toArray();	
		return $this->api_success(['data'=>$data]);		
	}
	/**
     * @name  商品二级三级分类
     */  
	public function product_type_two($id){
		$where = ['level'=>2,'status'=>1,'pid'=>$id];
		$data = $this->select('id','title')
				->orderBy('sort','asc')
				->orderBy('id','desc')
				->where($where)
				->get()
				->toArray();
		foreach($data as $k=>$v){
			$data_three = $this->select('id','title','image_id')
				->with([
				'image_one'=>function($query){
					$query->select('id','url','open');
				}      
				]) 
				->orderBy('sort','asc')
				->orderBy('id','desc')
				->where(['level'=>3,'status'=>1,'pid'=>$v['id']])
				->get()
				->toArray();
			$data[$k]['data_three'] = $this->image_list($data_three);	
		}					
		return $this->api_success(['data'=>$data]);		
	}
}